package com.ambientrs.bot.gui;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextPane log;
    private JTabbedPane tabs;
    private JMenuItem scriptStart;

    public Gui() {
        setSize(0x30a, 0x2b1);
        setLocationRelativeTo(null);
        setTitle("AmbientRS");
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        UIManager.put("nimbusBase", new Color(0x7f, 0x7f, 0x7f));
        UIManager.put("nimbusBlueGrey", new Color(0x77, 0x77, 0x77));
        UIManager.put("control", new Color(0x0, 0xff, 0x0));
        UIManager.put("text", new Color(0x0, 0x0, 0x0));
        UIManager.put("nimbusLightBackground", new Color(0x33, 0x33, 0x33));
        UIManager.put("nimbusFocus", new Color(0x44, 0xff, 0x44));
        UIManager.put("nimbusSelectionBackground", new Color(0x44, 0xff, 0x44));
        UIManager.put("nimbusSelectedText", new Color(0x0, 0x0, 0x0));
        UIManager.put("menuText", new Color(0x99, 0xff, 0x99));
        UIManager.put("menu", new Color(0x33, 0x33, 0x33));
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        JMenuBar menu = new JMenuBar();
        menu.setBounds(0x0, 0x0, 0x2fa, 0x19);
        JMenu bot = new JMenu("Bot");
        scriptStart = new JMenuItem("Start Script");
        MenuListener listener = new MenuListener();
        scriptStart.addActionListener(listener);
        bot.add(scriptStart);
        menu.add(bot);
        add(menu, BorderLayout.NORTH);
        log = new JTextPane();
        //log.setColor(new Color(0x99, 0xff, 0x99));
        log.setFocusable(false);
        log.setEditable(false);
        JScrollPane pane = new JScrollPane(log, 0x16, 0x1f);
        pane.setBounds(0x0, 0x222, 0x2fa, 0x69);
        add(pane, BorderLayout.SOUTH);
        tabs = new JTabbedPane();
        tabs.setBounds(0x0, 0x14, 0x2fa, 0x20b);
        tabs.setFocusable(false);
        add(tabs, BorderLayout.CENTER);
        tabs.add(null, "RuneScape");
    }

    public void init() {
        Loader c = new Loader();
        tabs.setComponentAt(0, c);
    }

    public void appendToLog(String toLog, boolean err)
            throws BadLocationException {
        StyledDocument doc = log.getStyledDocument();
        Element e = doc.getDefaultRootElement();
        AttributeSet attr = e.getAttributes().copyAttributes();
        doc.insertString(doc.getLength(), toLog, attr);
        MutableAttributeSet mattr = log.getInputAttributes();
        if (err)
            StyleConstants.setForeground(mattr, new Color(0xff, 0x77, 0x77));
        else
            StyleConstants.setForeground(mattr, new Color(0x99, 0xff, 0x99));
        doc.setCharacterAttributes(doc.getLength() - toLog.length(), doc.getLength(), mattr, false);
        log.setCaretPosition(doc.getLength());
    }

    public class MenuListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == scriptStart) {
                new ScriptSelector(null).setVisible(true);
            }

        }

    }

}
